	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_COST_FLIER_PRICE.php' );
	require_once( '../system/mdao/Class_M_COST_FLIER_PRICE_AREA.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );

	Class_PWD::valid_auth(AOKI_AUTH_COST_QR,$login['staff_auth']);

	$docsv_import = FALSE;
	
	$year4 = $_POST["year4"];
	$sort  = $_POST["sort"];
	
	$csvf = Class_FILE_CSV::get_upload_file();


	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
//echo 'aa';
		$rowall = Class_FILE_CSV::fget_rows($csvf);
	

		if  (!empty($rowall)) {
		$col_heads = array(
			''  , //年度
			''  , //種類
			'shop_cd'  , //店舗コード
			''  ,        //店名
			'b2'  , //B2価格
			'b3'  , //B3価格
			'b4'  , //B4価格
		);
		$sizearray = array(4 =>'b2', 5 => 'b3', 6 =>'b4');
		
		$csvheads    = array();
		$col_index   = array();

		foreach ($col_heads as $k => $v) {
			if (!empty($v)) {
				$csvheads[$k]  = $v;
				$col_index[$v] = $k;
			}
		}

		$key1 = $col_index['shop_cd'];//キーです、必ず仕様します
		if ($sort == 1){
			$dbshop_cd = 'shop_cd';
		}else {
			$dbshop_cd = 'pattern_cd';
		}
		
		$err = new Class_ERROR("","");
		if ($sort == '1') {
			$dao = new Class_M_COST_FLIER_PRICE;
			$shops     = Class_M_OTHER::get_shop_array(NULL);
		}else {
			$dao = new Class_M_COST_FLIER_PRICE_AREA;
			$patterns  = Class_M_OTHER::get_pattern_array( NULL, 3);
		}
		

		$ngline      = 0;
		$error_times = 0;
		$error_stop  = IMPORT_STOP_MAX_ERROR_TIMES;
		$errtext     = array();
		$isng        = FALSE;
		
		$chk  =  array(
			"shop_cd"     => array( "EXIST","NUM" ),
			"b2"          => array( "NUM" ),
			"b3"          => array( "NUM" ),
			"b4"          => array( "NUM" )
		 );
		
			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				//CSV内部重複不可チェック
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
						if ($row[$key1] == $vv[$key1]  ){
							$col1 = $key1 + 1;
							
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の[{$col1}]列目の値重複不可。</br>";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				}

				//一般チェック
				foreach ( $csvheads as $k=>$v ) {
					$msg =  $err->check( $rowall[$line][$k], $chk[$v ] );
					if (!empty($msg)) {
						$isng = TRUE;
						$col  = $k+1;
						$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
						$error_times++;
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
				if ($sort == 1){
					if (empty($shops[$row[$key1]])) {
						$col2 = $key2 + 1;
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][]   = "{$line}行{$col1}列目：{$row[$key1]}店舗マスタに存在しません。</br>";
					}	
				}else {
					if (empty($patterns[$row[$key1]])) {
						$col2 = $key2 + 1;
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][]   = "{$line}行{$col1}列目：{$row[$key1]}パッタンマスタに存在しません。</br>";
					}	
				}


				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}
			
			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);

				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );

				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );
				}
			} else {
				//DBへ更新
				try {
					$daodb = $dao->get_db();
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";
						foreach ( $rowall as $k=>$v ) {
							$i++;
							$percent = intval($i/$line * 100)."%";
							if  ( $percent != $percentold ) {
								Class_FILE_CSV::write_msg_doing( $percent ) ;
							}
							$percentold = $percent;
							$values  = array();
							$values["year4"] = $year4; 
							$values[$dbshop_cd] = $v[ $col_index["shop_cd"]];

							$key1values =  $values[$col_heads[$key1]];
							
							
							foreach ($sizearray as $sk=>$sv){								
								$values["price"] = empty($v[ $sk]) ? 0 : $v[ $sk] ;
								$values["size"]  =  $sv;
								$where = sprintf(" year4 = '%s' and {$col_heads[$key1]} = '%s' and size='{$sv}'  ", $year4, $key1values );
								
								if ( $dao->exits( $where ) ) {
									if (!($dao->editbywhere($where, $values ))) {
										throw  new Exception("mcost_flier_price_import DB更新");
									}
								} else {
									if ( !( $dao->add($values ) ) ) {
										throw  new Exception("mcost_flier_price_import DB登録");
									}
								}
							}
						}
					Class_FILE_CSV::write_msg_succed($line, "mcost_flier_price");
					$dao->commit_trans();

				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
				}

			}
		}else {

		$errHtml = "CSVファイルエラー1";
		Class_FILE_CSV::write_msg_error( $errHtml );

		}

		// ロケール情報の復元
		setlocale(LC_ALL, $locale);

	}else {

		$errHtml = "CSVファイルエラー2";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
